<?php
/* @var $this BookingController */

$this->breadcrumbs = array(
    'Booking',
);
?>
<h1>Book Service</h1>
<?php
$cs = Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->getBaseUrl() . '/js/calendar/_loader.js');
?>

<?php
$service = null;
if (isset($model)) {

    echo '<div style="float:left;margin-top:5px;">' . $model->name . '(' . $model->duration . ')' . '</div>';
}
if (isset($resources)) {
    $url = Yii::app()->createUrl('booking/resourceCalenderConfig');
    echo '<select class="cmn_input_drp ml10" id="resource_option" onchange="loadResource(\'' . $url . '\',this.options[this.selectedIndex].value,\'' . $model->id . '\')"><option value="0">Select Resource</option>';
    foreach ($resources as $value) {
        echo '<option value="' . $value->id . '">' . $value->profile->name . '</option>';
    }
    echo '</select>';
} else {
    echo 'No Resource Available ';
}

$this->renderPartial('/common/calendar');
?>
<div id='calendar' style="margin-top:20px;"></div>
<script type="text/javascript">
    // $('confim_dialog').hide();
    function loadResource(url, resource_id, service_id) {
        if (resource_id === '0') {
            document.getElementById('calendar').innerHTML = "";
            return;
        }
        document.getElementById('calendar').innerHTML = "";
        document.getElementById('calendar').innerHTML = "Loading Calender.....";
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        var _date = y + '-' + m + 1 + '-' + d;
        $.get(url, {resource: resource_id, date: _date, service: service_id},
        function(data) {
            document.getElementById('calendar').innerHTML = "";
            var result = JSON.parse(data);
            context = new Array();
            context['resource_id'] = resource_id;
            context['service_id'] = service_id;
            showCalender(bookNow, result, context);
        }
        );
    }
    function  bookNow(start, context) {
        posturl =<?php echo '"' . Yii::app()->createUrl('booking/bookAppointment') . '";'; ?>
        document.getElementById('start_time').innerHTML = "Start Time : " + start;
        content = '<input id="resource" value="' + context['resource_id'] + '"/><input id="start" value="' + start + '"/><input id="service" value="' + context['service_id'] + '"/>';
        document.getElementById('details').innerHTML = content;
        confmMsg =<?php echo '" Aru you sure to shedule \n\' ' . $model->name . ' \'  on \n" '; ?> + start;
        if (confirm(confmMsg)) {
            $.post(posturl, {resource: context['resource_id'], start: getFormatedDate(start), service: context['service_id']}, function(result, status) {
                var jresult = JSON.parse(result);
        if (jresult.status === 'success') {
                    $("#calendar").fullCalendar('refetchEvents');
                    alert('Booking Success');
                } else {
                    alert("Booking failed : " + jresult.message);
                }
            }).fail(function(data) {
                alert("Booking failed :"+data);
            });

        }
    }
    function getFormatedDate(date) {
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        var h = date.getHours();
        var min = date.getMinutes();
        var s = date.getSeconds();
        return y + '-' + (parseInt(m) + 1) + '-' + d + ' ' + h + ':' + min + ':' + s;
    }
</script>


<!--event creation confirmation dialog-->
<div id="confim_dialog" style="position: absolute;width: 500px;height: 500px;display: none">
    <span>Booking Confirmation </span>
    <span>Service Name <?php echo $model->name; ?></span>
    <span id="start_time"></span>
    <form action=<?php echo '"' . Yii::app()->createUrl('booking/bookAppointment') . '"'; ?> >
        <div id="details"></div>
        <input type="submit" value="Submit"/>
    </form>
</div>
</p>
